package com.acc.store.mapper;

import com.acc.store.entity.Orders;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper extends BaseMapper<Orders> {
    @Select("SELECT d.date, IFNULL(SUM(o.total_price), 0) AS daily_total\n" +
            "FROM (\n" +
            "         SELECT DATE_FORMAT(date, '%Y-%m-%d') AS date\n" +
            "         FROM (\n" +
            "                  SELECT CURDATE() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY AS date\n" +
            "                  FROM (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS a\n" +
            "                           CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS b\n" +
            "                           CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS c\n" +
            "              ) AS dates\n" +
            "         WHERE date >= DATE_FORMAT(NOW(), '%Y-%m-01')\n" +
            "           AND date <= LAST_DAY(NOW())\n" +
            "     ) AS d\n" +
            "         LEFT JOIN orders o ON DATE(o.create_time) = d.date\n" +
            "GROUP BY d.date\n" +
            "ORDER BY d.date ASC;")
    List<Map<String, BigDecimal>> getLine();
}




